
package com.monlyu.action;

import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.monlyu.config.AppArgs;
import com.monlyu.util.ActionUtil;
import com.monlyu.util.JsonUtil;
import com.monlyu.util.LogUtil;
import com.monlyu.util.StringUtil;
import com.monlyu.util.WebOptUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author monlyu
 * @since 2009-12-15
 */
public abstract class BaseAction extends ActionSupport {

    protected final Log log = LogFactory.getLog(getClass());

    /** 图片的宽度 */
    private int width = 600;
    /** 图片的高度 */
    private int height = 400;

    /** 文件名 */
    protected String filename;

    /** 文件流 */
    protected InputStream inputStream;

    /** 文件的min类型 */
    protected String contentType;

    /** 返回的jsp目标对象 */
    public String target;

    public void setDecType(int decType){
        this.decType = decType;
    }

    /** 标志,添加或删除 */
    protected String flag = "a";

    protected int decType = ActionUtil.SITEMESH_ALL; // 页面装饰decoration
    protected int titleType = ActionUtil.SITEMESH_TITLE;

    public int getTitleType(){
        return titleType;
    }

    public int getDecType(){
        return decType;
    }

    protected void info(String info){
        log.debug(info);
    }

    public void setTarget(String target){
        this.target = target;
    }

    /**
     * 跳转到某个Action(不可以加参数)
     * 
     * @param action
     */
    public String redirectAction(String action){
        setTarget(action);
        return "redirectAction";
    }

    /**
     * 跳到下载的页面
     */
    public String download(){
        return "download";
    }

    private void printStack(String path){
        LogUtil.logWithTime(getClass(), WebOptUtil.curUser().getNickName() + " 访问>" + path);
    }

    /**
     * 跳转到某个具体的url(可以加参数)
     * 
     * @param url
     */
    public String redirect(String url){
        setTarget(url);
        return "redirect";
    }

    /**
     * 跳到某个页面<br>
     * 如: com.monlyu.action.AAA.XXXAction<br>
     * 跳到: AAA\path.jsp
     * 
     * @param path
     */
    public String view(String path){
        setTarget(path);
        return "view";
    }

    /**
     * 跳到具体的某个jsp页面（写完整的路径，如：home\home.jsp）
     * 
     * @param page
     */
    public String cstm(String page){
        setTarget(page);
        return "cstm";
    }

    /**
     * 返回Ajax值 ""
     * 
     * @since 2010-1-14
     */
    protected String ajax(){
        return ajax("");
    }

    /**
     * 把对象转化为Json对象
     * 
     * @since 2010-1-17
     */
    protected String json(Object obj){
        return addDataInThread("json" + JsonUtil.obj2Json(obj));
    }

    /**
     * @since 2010-1-14
     */
    protected String ajax(Object data){
        return addDataInThread("text" + data);
    }

    /**
     * 把后台传输的数据线程中,让前台能够获取得到后台传过来的数据
     * 
     * @since 2010-1-14
     */
    private String addDataInThread(Object data){
        decType = ActionUtil.SITEMESH_AJAX;
        WebOptUtil.addData2Thread(data);
        return "ajaxData";
    }

    /**
     * 参数为一个数组,第一个参数为 宽度 ,第二个为 高度 默认值 600 * 400
     * 
     * @since 2010-1-16
     */
    protected String showChart(final int... args){
        if (args != null && args.length > 0) {
            this.width = args[0];
            if (args.length > 1) {
                this.height = args[1];
            }
        }
        return "showChart";
    }

    /** *** Get Set 方法 *** */
    public int getWidth(){
        return width;
    }

    public int getHeight(){
        return height;
    }

    public String getFilename(){
        return StringUtil.str2Iso8859_1(filename);
    }

    public InputStream getInputStream(){
        return inputStream;
    }

    public String getContentType(){
        return contentType;
    }

    public void setFilename(String filename){
        this.filename = filename;
    }

    public int getDayLeft(){
        return AppArgs.DAY_LEFT;
    }
}
